package com.tvserials.batch.processor;

import com.tvserials.notification.dto.Episode;
import com.tvserials.notification.dto.Serial;
import com.tvserials.notification.exceptions.NetworkConnectionException;
import com.tvserials.notification.exceptions.PageParseException;
import com.tvserials.notification.parser.PageParser;
import com.tvserials.notification.parser.PageParserFactory;
import com.tvserials.notification.vo.SourceType;
import com.tvserials.notification.vo.TvEpisodeFilterVO;
import com.tvserials.service.EpisodeService;
import com.tvserials.service.SerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.*;

@Component
public class PageProcessor {

    @Autowired
    private EpisodeService episodeService;

    @Autowired
    private SerialService serialService;

    @Scheduled(cron = "${page.processor.cron}")
    public void processSerials() {
        System.out.println("Processing serials");
        List<PageParser> pageParsers = Arrays.asList(PageParserFactory.getPageParser(SourceType.ALL));

        Set<Serial> databaseSerials = getUniqueSerialsSet();
        List<Serial> newSerials = new ArrayList<Serial>();

        for (PageParser parser : pageParsers) {
            try {
                List<Serial> serials = parser.parseSerialsList();
                System.out.println(serials);
                newSerials.addAll(getNewSerials(databaseSerials, serials));
            } catch (NetworkConnectionException e) {
                e.printStackTrace();
            }
        }

        serialService.save(newSerials);
    }

    private List<Serial> getNewSerials(Set<Serial> databaseSerials, List<Serial> serials) {
        if (databaseSerials.containsAll(serials)) {
            return new ArrayList<Serial>();
        }
        serials.removeAll(databaseSerials);
        return serials;
    }

    @Scheduled(cron = "${page.processor.cron}")
    public void processEpisodes() {
        System.out.println("Processing episodes");
        List<PageParser> pageParsers = PageParserFactory.getAllParsers();

        TvEpisodeFilterVO filterVO = new TvEpisodeFilterVO();
        Date lastSyncDate = episodeService.getLastSyncDate();
        filterVO.setLastSyncDate(lastSyncDate);

        for (PageParser parser : pageParsers) {
            try {
                List<Episode> episodes = parser.parsePage(filterVO);
                System.out.println(episodes);
            } catch (NetworkConnectionException e) {
                e.printStackTrace();
            } catch (PageParseException e) {
                e.printStackTrace();
            }
        }
    }

    private Set<Serial> getUniqueSerialsSet() {
        Set<Serial> allUniqueSerials = new HashSet<Serial>();
        List<Serial> serialsList = serialService.findAll();
        if (serialsList != null) {
            allUniqueSerials.addAll(serialsList);
        }
        return allUniqueSerials;
    }
}
